package com.yhp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisNurseProjectMapper;
import com.system.common.pojo.BisNurseProject;
import com.system.common.pojo.BisNurseProjectExample;
import com.system.common.pojo.BisNurseProjectExample.Criteria;
import com.system.common.pojo.SysUser;

@Service
public class BisNurseProjectServiceImpl implements BisNurseProjectService {

	@Autowired
	private BisNurseProjectMapper bisNurseProjectMapper;

	/**
	 * 综合查询
	 */
	@Override
	public List<BisNurseProject> selectByExample(Map<String, Object> map) {
		String projectId = (String) map.get("projectId");
		String projectName = (String) map.get("projectName");
		String status = (String) map.get("status");
		String typeId = (String) map.get("typeId");
		BisNurseProjectExample example = new BisNurseProjectExample();
		Criteria criteria = example.createCriteria(); // 初始化一个查询条件
		if (StringUtils.isNotBlank(projectName)) {
			criteria.andProjectNameLike("%" + projectName + "%");
		}
		if (StringUtils.isNotBlank(projectId)) {
			criteria.andProjectIdEqualTo(projectId);
		}
		if (StringUtils.isNotBlank(status)) {
			criteria.andStatusEqualTo(status);
		}
		if (StringUtils.isNotBlank(typeId)) {
			criteria.andTypeIdEqualTo(typeId);
		}
		List<BisNurseProject> bisNurseProjectList=new ArrayList<>();
		List<BisNurseProject> selectByExample = bisNurseProjectMapper.selectByExample(example);
		for (BisNurseProject bisNurseProject : selectByExample) {
			if (bisNurseProject.getIsDel().equals("0")) {
				bisNurseProjectList.add(bisNurseProject);
			}
		}
		return bisNurseProjectList;
	}

	/**
	 * 根据ID单个查询
	 */
	@Override
	public BisNurseProject selectByPrimaryKey(String projectId) {
		return bisNurseProjectMapper.selectByPrimaryKey(projectId);
	}

	/**
	 * 添加
	 */
	@Override
	public int insert(BisNurseProject bisNurseProject,HttpSession session) {
		// 项目编号 是否删除 创建时间 创建用户编号 修改时间 修改用户编号
		SysUser user = (SysUser) session.getAttribute("user");
		Snowflake sf=new Snowflake(0, 0);
		bisNurseProject.setProjectId(sf.nextId()+"");
		bisNurseProject.setIsDel("0");
		bisNurseProject.setCreateTime(new Date());
		bisNurseProject.setCreateUserId(user.getUserId());
		return bisNurseProjectMapper.insert(bisNurseProject);
	}

	/**
	 * 修改
	 */
	@Override
	public int updateByPrimaryKey(BisNurseProject bisNurseProject,HttpSession session) {
		BisNurseProject selectByPrimaryKey = bisNurseProjectMapper.selectByPrimaryKey(bisNurseProject.getProjectId());
		SysUser user = (SysUser) session.getAttribute("user");
		bisNurseProject.setCreateTime(selectByPrimaryKey.getCreateTime());
		bisNurseProject.setCreateUserId(selectByPrimaryKey.getCreateUserId());
		bisNurseProject.setModifyTime(new Date());
		bisNurseProject.setModifyUserId(user.getUserId());
		return bisNurseProjectMapper.updateByPrimaryKey(bisNurseProject);
	}

	@Override
	public List<BisNurseProject> selectByExample2() {
		BisNurseProjectExample example = new BisNurseProjectExample();
		return bisNurseProjectMapper.selectByExample(example);
	}

	/**
	 * 修改查询
	 */
	@Override
	public boolean selectByProjectId(String projectId) {
		List<Map<String, Object>> bagIds = bisNurseProjectMapper.selectByProjectId(projectId);
		if (bagIds.size()>0) {//该项目有礼包使用
			String state="0";
			for (Map<String, Object> map : bagIds) {//通过循环对比每一个礼包的状态
				String status = bisNurseProjectMapper.selectByBagIds((String)map.get("bag_id"));
				if (!status.equals("2")) {
					//如果有一个礼包在售或者是待上架就改变状态
					state="1";
					break;
				}
			}
			if (state=="1") {
				return false;
			}
		}
		return true;
	}
}
